<template>
    <div class="reg-wrap">
        <div class="ms-reg">
            <div class="ms-title">添加货物类型</div>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="名称" prop="cargotype">
                    <el-input type="text" v-model="ruleForm.cargotype" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="设备类型" prop="cate">
                    <el-input type="text" v-model="ruleForm.cate" autocomplete="off" placeholder="MOT_U202"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="desc">
                    <el-input type="textarea" v-model="ruleForm.desc" autocomplete="off" :autosize="{ minRows: 2, maxRows: 4 }"></el-input>
                </el-form-item>
                <el-form-item label="记录间隔(分)" prop="interval">
                    <el-input-number
                        type="text"
                        v-model="ruleForm.interval"
                        autocomplete="off"
                        controls-position="right"
                        :min="0.5"
                        :step="0.5"
                    ></el-input-number>
                </el-form-item>
                <el-form-item label="理想温度范围" prop="scope">
                    <el-input type="text" v-model="ruleForm.scope" autocomplete="off" placeholder="输入最低和最高温度，逗号间隔"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
                    <!-- <el-button type="primary" @click="update('ruleForm')" v-else>编辑完成</el-button> -->
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div>
            <div style="width:400px" class="div">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>报警设置-主警报1</span>
                        <el-switch
                            v-model="value_list.value0"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            style="float: right; padding: 3px 0"
                        >
                        </el-switch>
                    </div>
                    <div>
                        <div class="text item">
                            报警类型：
                            <el-select v-model="type_list.value0" placeholder="请选择">
                                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                            </el-select>
                        </div>
                        <br /><br />
                        <div class="text item">
                            温度范围：
                            <el-input-number
                                v-model="scope_list.num0"
                                controls-position="right"
                                @change="handleChange"
                                :min="0"
                                size="mini"
                                :step="0.1"
                            ></el-input-number>
                        </div>
                        <br />
                        <div class="text item">
                            阈值：
                            <br /><br />
                            <div class="div1">
                                <el-input v-model="input_list.input0" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </div>
                </el-card>
            </div>
            <div style="width:400px" class="div">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>报警设置-主警报2</span>
                        <el-switch
                            v-model="value_list.value1"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            style="float: right; padding: 3px 0"
                        >
                        </el-switch>
                    </div>
                    <div>
                        <div class="text item">
                            报警类型：
                            <el-select v-model="type_list.value1" placeholder="请选择">
                                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                            </el-select>
                        </div>
                        <br /><br />
                        <div class="text item">
                            温度范围：
                            <el-input-number
                                v-model="scope_list.num1"
                                controls-position="right"
                                @change="handleChange"
                                :min="0"
                                size="mini"
                                :step="0.1"
                            ></el-input-number>
                        </div>
                        <br />
                        <div class="text item">
                            阈值：
                            <br /><br />
                            <div class="div1">
                                <el-input v-model="input_list.input1" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </div>
                </el-card>
            </div>
            <div style="width:400px" class="div">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>报警设置-主警报3</span>
                        <el-switch
                            v-model="value_list.value2"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            style="float: right; padding: 3px 0"
                        >
                        </el-switch>
                    </div>
                    <div>
                        <div class="text item">
                            报警类型：
                            <el-select v-model="type_list.value2" placeholder="请选择">
                                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                            </el-select>
                        </div>
                        <br /><br />
                        <div class="text item">
                            温度范围：
                            <el-input-number
                                v-model="scope_list.num2"
                                controls-position="right"
                                @change="handleChange"
                                :min="0"
                                size="mini"
                                :step="0.1"
                            ></el-input-number>
                        </div>
                        <br />
                        <div class="text item">
                            阈值：
                            <br /><br />
                            <div class="div1">
                                <el-input v-model="input_list.input2" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </div>
                </el-card>
            </div>
            <div style="width:400px" class="div">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>报警设置-主警报4</span>
                        <el-switch
                            v-model="value_list.value3"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            style="float: right; padding: 3px 0"
                        >
                        </el-switch>
                    </div>
                    <div>
                        <div class="text item">
                            报警类型：
                            <el-select v-model="type_list.value3" placeholder="请选择">
                                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                            </el-select>
                        </div>
                        <br /><br />
                        <div class="text item">
                            温度范围：
                            <el-input-number
                                v-model="scope_list.num3"
                                controls-position="right"
                                @change="handleChange"
                                :min="0"
                                size="mini"
                                :step="0.1"
                            ></el-input-number>
                        </div>
                        <br />
                        <div class="text item">
                            阈值：
                            <br /><br />
                            <div class="div1">
                                <el-input v-model="input_list.input3" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </div>
                </el-card>
            </div>
            <div style="width:400px" class="div">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>报警设置-主警报5</span>
                        <el-switch
                            v-model="value_list.value4"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            style="float: right; padding: 3px 0"
                        >
                        </el-switch>
                    </div>
                    <div>
                        <div class="text item">
                            报警类型：
                            <el-select v-model="type_list.value4" placeholder="请选择">
                                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                            </el-select>
                        </div>
                        <br /><br />
                        <div class="text item">
                            温度范围：
                            <el-input-number
                                v-model="scope_list.num4"
                                controls-position="right"
                                @change="handleChange"
                                :min="0"
                                size="mini"
                                :step="0.1"
                            ></el-input-number>
                        </div>
                        <br />
                        <div class="text item">
                            阈值：
                            <br /><br />
                            <div class="div1">
                                <el-input v-model="input_list.input4" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </div>
                </el-card>
            </div>
            <div style="width:400px" class="div">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>报警设置-主警报6</span>
                        <el-switch
                            v-model="value_list.value5"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            style="float: right; padding: 3px 0"
                        >
                        </el-switch>
                    </div>
                    <div>
                        <div class="text item">
                            报警类型：
                            <el-select v-model="type_list.value5" placeholder="请选择">
                                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                            </el-select>
                        </div>
                        <br /><br />
                        <div class="text item">
                            温度范围：
                            <el-input-number
                                v-model="scope_list.num5"
                                controls-position="right"
                                @change="handleChange"
                                :min="0"
                                size="mini"
                                :step="0.1"
                            ></el-input-number>
                        </div>
                        <br />
                        <div class="text item">
                            阈值：
                            <br /><br />
                            <div class="div1">
                                <el-input v-model="input_list.input5" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </div>
                </el-card>
            </div>
        </div>
    </div>
</template>

<script>
import { add_cargotype, put_cargotype } from '../../api/api.js';
export default {
    name: 'login',
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入名称'));
            } else {
                if (this.ruleForm.password !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入设备类型'));
            } else {
                callback();
            }
        };
        var validatePass3 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入描述'));
            } else {
                callback();
            }
        };
        var validatePass5 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入记录间隔'));
            } else {
                callback();
            }
        };
        var validatePass6 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入温度范围'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                cargotype: '',
                cate: 'MOT_U202',
                desc: '',
                interval: 5,
                scope: ''
            },
            rules: {
                cargotype: [{ validator: validatePass, trigger: 'blur' }],
                cate: [{ validator: validatePass2, trigger: 'blur' }],
                desc: [{ validator: validatePass3, trigger: 'blur' }],
                interval: [{ validator: validatePass5, trigger: 'blur' }],
                scope: [{ validator: validatePass6, trigger: 'blur' }]
            },
            cid: '',
            // 启动开关列表
            value_list: { value0: true, value1: false, value2: false, value3: false, value4: false, value5: false },
            // 报警类型列表
            type_list: { value0: '', value1: '', value2: '', value3: '', value4: '', value5: '' },
            // 温度范围列表
            scope_list: { num0: 0, num1: 0, num2: 0, num3: 0, num4: 0, num5: 0 },
            // 阈值列表
            input_list: { input0: '', input1: '', input2: '', input3: '', input4: '', input5: '' },
            options: [
                { value: '单温上限-单个事件', label: '单温上限-单个事件' },
                { value: '单温下限-单个事件', label: '单温下限-单个事件' },
                { value: '单温度范围-累计事件', label: '单温度范围-累计事件' }
            ]
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                var str = this.ruleForm.scope.split('，');
                str = JSON.stringify(str);
                if (valid) {
                    add_cargotype({
                        cid: '',
                        cargotype: this.ruleForm.cargotype,
                        cate: this.ruleForm.cate,
                        desc: this.ruleForm.desc,
                        interval: this.ruleForm.interval,
                        scope: str,
                        value_list: this.value_list,
                        type_list: this.type_list,
                        scope_list: this.scope_list,
                        input_list: this.input_list
                    }).then(res => {
                        this.$message(res.data.msg);
                    });
                } else {
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        update(formName) {
            this.$refs[formName].validate(valid => {
                var str = this.ruleForm.scope.split('，');
                str = JSON.stringify(str);
                if (valid) {
                    if (this.cid) {
                        put_cargotype({
                            cid: this.cid,
                            cargotype: this.ruleForm.cargotype,
                            cate: this.ruleForm.cate,
                            desc: this.ruleForm.desc,
                            interval: this.ruleForm.interval,
                            scope: str,
                            value_list: this.value_list,
                            type_list: this.type_list,
                            scope_list: this.scope_list,
                            input_list: this.input_list
                        }).then(res => {
                            this.$message(res.data.msg);
                        });
                    }
                } else {
                    return false;
                }
            });
        },
        handleChange(value) {
            console.log(value);
        }
    },
    mounted() {},
    watch: {
        $route(to, from) {
            // 获取货物类型信息
            this.ruleForm.cargotype = this.$route.query.cargotype;
            this.ruleForm.cate = this.$route.query.cate;
            this.ruleForm.desc = this.$route.query.desc;
            this.cid = this.$route.query.cid;
        }
    }
};
</script>

<style scoped>
.reg-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-reg {
    /* position: absolute; */
    /* left: 50%;
    top: 50%; */
    width: 350px;
    /* margin: -190px 0 0 -175px; */
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.reg-btn {
    text-align: center;
}
.reg-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.reg-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
.div1 {
    width: 150px;
}
.div {
    float: left;
}
</style>